var vm = new Vue({
    el:"#app",
    data(){
        return {
            NowDate:'',
            Store:'',
            Bm:'',
            //搜索条件
            WhNameOrNo:'',
            formItem: {
                WarehouseNo: '',
                Name:'',
                Store:'',
                UseBm:'',
                Remark:''
            },
            EditFormItem:{
                WarehouseNo: '',
                Name:'',
                Store:'',
                UseBm:'',
                Remark:''
            },
            modal1:false, //添加经销商弹框
            modal2:false, //编辑经销商弹框
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '仓库编号',
                    key: 'WarehouseNo',
                    align: 'center',
                },
                {
                    title: '所属店面',
                    key: 'Store',
                    align: 'center',
                    width:300
                },
                {
                    title: '仓库名称',
                    key: 'Name',
                    align: 'center',
                    width:300
                },
                {
                    title: '使用部门',
                    key: 'UseBm',
                    align: 'center',
                    width:300
                },
                {
                    title: '描述',
                    key: 'Remark',
                    align: 'center',
                },
                {   title: '操作',
                    key: 'action',
                    align: 'center',
                    width:150,
                    render: (h, params) => {
                        return h('div', [
                            h('a', {
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var self = this;
                                        if(self.Bm!="财务"){
                                            toastr.warning("无该操作权限");
                                            return false;
                                        }
                                        this.EditFormItem.WarehouseNo = params.row.WarehouseNo;
                                        this.EditFormItem.Name = params.row.Name;
                                        this.EditFormItem.Store = params.row.Store;
                                        this.EditFormItem.UseBm = params.row.UseBm;
                                        this.EditFormItem.Remark = params.row.Remark;
                                        this.modal2=true;
                                    }
                                }
                            },'编辑'),
                            h('span', {
                            },'|'),
                            h('a', {
                                style: {
                                    marginLeft: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var self = this;
                                        if(self.Bm!="财务"){
                                            toastr.warning("无该操作权限");
                                            return false;
                                        }
                                        var CreateTime = params.row.CreateTime;
                                        var CreateStrArr = CreateTime.split(" ");
                                        var CreateDate = CreateStrArr[0];
                                        var NowDate = self.NowDate;
                                        if(CreateDate!=NowDate){
                                            toastr.warning("只能操作当天数据");
                                            return false;
                                        }

                                        var WarehouseNo = params.row.WarehouseNo;
                                        this.confirm(WarehouseNo);
                                    }
                                }
                            },'删除')
                        ])
                    }
                }
            ],
            data1: [],

        }
    },
    created(){
        this.initThisPage();
    },
    methods:{
        ok () {
        },
        cancel () {
        },
        confirm (WarehouseNo) {
            var self = this;
            this.$Modal.confirm({
                title: '提示',
                content: '<p>是否删除</p>',
                onOk: () => {
                    self.delWh(WarehouseNo);
                },
                onCancel: () => {
                    toastr.info("取消删除");
                }
            });
        },
        //打开添加经销商弹框
        openModel1(){
            this.formItem.WarehouseNo = "KF-"+this.uuid(8, 16);
            this.formItem.Store = this.Store;
            this.modal1 = true;
        },
        querytable(){
            var self = this;
            var info ;
            var Store = self.Store;
            var WhNameOrNo = self.WhNameOrNo;
            var jsondata={"WhNameOrNo":WhNameOrNo};
            if(Store!="运营管理中心"){
                jsondata.Store = Store;
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goWh/queryAllWh", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.ResultList);
                },
                error: function() {},
            });
            if(info){
                self.data1 = info;
            }
        },
        saveWh(){
            var self = this;
            var WarehouseNo = self.formItem.WarehouseNo;
            var Name = self.formItem.Name;
            Name = Name.trim();
            if(Name==null||Name==""){
                toastr.warning("请录入仓库名称");
                return false;
            }
            var Store = self.formItem.Store;
            if(Store==null||Store==""){
                toastr.warning("请选择店面");
                return false;
            }
            var UseBm = self.formItem.UseBm;
            if(UseBm==null||UseBm==""){
                toastr.warning("请选择使用部门");
                return false;
            }
            var Remark = self.formItem.Remark;
            var jsondata = {
                "WarehouseNo":WarehouseNo,
                "Name":Name,
                "Store":Store,
                "UseBm":UseBm,
                "Remark":Remark
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goWh/saveWh", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('保存成功');
                        self.formItem.WarehouseNo = "";
                        self.formItem.Name = "";
                        self.formItem.Store = "";
                        self.formItem.UseBm = "";
                        self.formItem.Remark = "";
                        self.querytable();
                    }else if(data.Status=='-1'){
                        toastr.warning('保存失败，仓库已存在');
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        // 编辑弹框
        editWh(){
            var self = this;
            var WarehouseNo = self.EditFormItem.WarehouseNo;
            var Name = self.EditFormItem.Name;
            Name = Name.trim();
            if(Name==null||Name==""){
                toastr.warning("请录入仓库名称");
                return false;
            }
            var Store = self.EditFormItem.Store;
            if(Store==null||Store==""){
                toastr.warning("请选择店面");
                return false;
            }
            var UseBm = self.EditFormItem.UseBm;
            if(UseBm==null||UseBm==""){
                toastr.warning("请选择使用部门");
                return false;
            }
            var Remark = self.EditFormItem.Remark;
            var jsondata = {
                "WarehouseNo":WarehouseNo,
                "Name":Name,
                "Store":Store,
                "UseBm":UseBm,
                "Remark":Remark
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goWh/editWh", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('编辑成功');
                        self.querytable();
                    }else if(data.Status=='-1'){
                        toastr.warning('编辑失败，仓库已存在');
                    }else{
                        toastr.error('编辑失败，请重新编辑');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        delWh(WarehouseNo){
            var self = this;
            var jsondata = {
                "WarehouseNo":WarehouseNo,
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goWh/delWh", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success("删除成功");
                        self.querytable();
                    }else{
                        toastr.error('删除失败，请刷新页面后重试');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goLogin/initIndexPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                async:false,
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="../../login.html";
                    }else{
                        self.NowDate=data.nowdate;
                        self.Store = data.Store;
                        self.Bm = data.Bm;
                        if(data.Bm!="财务"){
                            $("#SaveWhIsOrNot").attr("style","display:none");
                        }else{
                            $("#SaveWhIsOrNot").attr("style","");
                        }
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },

        //手机号验证
        isPoneAvailable(str) {
            var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        //uuid
        uuid(len, radix) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [], i;
            radix = radix || chars.length;

            if (len) {
                // Compact form
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
            } else {
                // rfc4122, version 4 form
                var r;

                // rfc4122 requires these characters
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';

                // Fill in random data. At i==19 set the high bits of clock sequence as
                // per rfc4122, sec. 4.1.5
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random()*16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }

            return uuid.join('');
        }
    }
})